const { sequelize } = require('./models');
const UserService = require('./services/user.service');

async function main() {
  try {
    console.log('数据库连接测试');
    // 测试数据库连接
    await sequelize.authenticate();
    console.log('数据库连接成功');

    // 同步模型到数据库
    // await sequelize.sync({ force: true });
    // console.log('数据库同步完成');

    // 示例操作

    //create 测试
    const user1 = await UserService.createUser({
      firstName: 'John',
      lastName: 'Doe',
      email: 'john@example.com',
      age: 30
    });
    
    console.log('创建用户:', user1.toJSON());

    const allUsers = await UserService.getAllUsers();
    console.log('所有用户:', allUsers.map(u => u.toJSON()));

    const updatedUser = await UserService.updateUser(user1.id, {
      age: 31
    });
    console.log('更新后的用户:', updatedUser.toJSON());

    const deleteResult = await UserService.deleteUser(user1.id);
    console.log('删除结果:', deleteResult);
    
  } catch (error) {
    console.error('操作失败:', error);
  } finally {
    await sequelize.close();
  }
}

main();